package com.mall.config;

import com.mall.common.Constant;
import com.mall.filter.AdminFilter;
import com.mall.filter.UserFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
@Configuration
public class MyWebConfig {
//    在登陆的时候进行身份校验
    @Bean
    public AdminFilter adminFilter(){
        return new AdminFilter();
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBeanAdmin(@Autowired AdminFilter adminFilter){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(adminFilter);
//        filterRegistrationBean.setUrlPatterns(Arrays.asList("/admin/category/*","/admin/product/*"));
//        添加过滤路径
        filterRegistrationBean.addUrlPatterns("/admin/category/*");
        filterRegistrationBean.addUrlPatterns("/admin/product/*");
        filterRegistrationBean.addUrlPatterns("/admin/upload/file");
        filterRegistrationBean.addUrlPatterns("/admin/order/*");
        return filterRegistrationBean;
    }
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/images/**")
                        .addResourceLocations("file:"+ Constant.UPLOAD_DIR);
            }
        };
        return webMvcConfigurer;
    }

//    配置用户过滤器
@Bean
public UserFilter userFilter(){
    return new UserFilter();
}
    @Bean
    public FilterRegistrationBean filterRegistrationBeanUser(@Autowired UserFilter userFilter){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(userFilter);
        filterRegistrationBean.addUrlPatterns("/cart/*");
        filterRegistrationBean.addUrlPatterns("/order/*");
        return filterRegistrationBean;
    }
}
